<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="所属门店" prop="userName">
      <treeselect style="width: 250px;" v-model="queryParams.shopId" :options="deptList" :show-count="true" placeholder="请选择门店" />
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="initData"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="12">
        <line-chart
          v-if="showDishesCount"
          chartTitle="每天出餐数量"
          :chart-data="dishesCountChartData"
          :chart-config="dishesCountChartConfig"
          height="400px"
        />
      </el-col>
      <el-col :span="12">
        <line-chart
          v-if="showDishesAvgTime"
          chartTitle="每天出餐平均耗时"
          :chart-data="dishesAvgTimeChartData"
          :chart-config="dishesAvgTimeChartConfig"
          height="400px"
        />
      </el-col>
    </el-row>

    <el-table :data="tableData" v-loading="loading" stripe>
      <el-table-column prop="date" label="日期"> </el-table-column>
      <el-table-column prop="hour" label="小时"> </el-table-column>
      <el-table-column label="出餐菜品数量">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" v-if="scope.row.itemTypeMaps['A']" @click="queryDishesInfo('A','1',scope.row)">
            A({{ scope.row.itemTypeMaps["A"] }})</el-link
          >
          <el-link type="primary" :underline="false" v-if="scope.row.itemTypeMaps['B']" @click="queryDishesInfo('B','1',scope.row)">
            B({{ scope.row.itemTypeMaps["B"] }})</el-link
          >
          <el-link type="primary" :underline="false" v-if="scope.row.itemTypeMaps['C']" @click="queryDishesInfo('C','1',scope.row)">
            C({{ scope.row.itemTypeMaps["C"] }})</el-link
          >
        </template>
      </el-table-column>
      <el-table-column label="被迫出餐菜品数量">
        <template slot-scope="scope">
            <el-link type="primary" :underline="false" v-if="scope.row.timeOutItemTypeMaps['A']" @click="queryDishesInfo('A','2',scope.row)">
              A({{ scope.row.timeOutItemTypeMaps["A"] }})
            </el-link> 
            <el-link type="primary" :underline="false" v-if="scope.row.timeOutItemTypeMaps['B']" @click="queryDishesInfo('B','2',scope.row)">
            B({{ scope.row.timeOutItemTypeMaps["B"] }})</el-link
          >
          <el-link type="primary" :underline="false" v-if="scope.row.timeOutItemTypeMaps['C']" @click="queryDishesInfo('C','2',scope.row)">
            C({{ scope.row.timeOutItemTypeMaps["C"] }})</el-link
          >
        </template>
      </el-table-column>
      <el-table-column prop="totalCookTime" label="配菜耗时">
        <template slot-scope="scope">
          {{ (scope.row.totalCookTime / 60).toFixed(2) }}分钟
        </template>
      </el-table-column>
      <el-table-column prop="totalServeTime" label="传菜耗时">
        <template slot-scope="scope">
          {{ (scope.row.totalServeTime / 60).toFixed(2) }}分钟
        </template>
      </el-table-column>
      <el-table-column prop="totalConsumeTime" label="出餐耗时">
        <template slot-scope="scope">
          {{ (scope.row.totalConsumeTime / 60).toFixed(2) }}分钟
        </template>
      </el-table-column>
      <el-table-column prop="dishesCount" label="平均耗时">
        <template slot-scope="scope">
          {{
            ((scope.row.totalConsumeTime / scope.row.totalDishesCount)/60).toFixed(2)
          }}分钟
        </template></el-table-column
      ></el-table
    >
    <pagination
      :total="total"
      :show="total > 0"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog  :visible.sync="dialogTableVisible"  width="1200px" :close-on-click-modal="false">
  <el-table :data="gridData">
    <el-table-column type="index" label="序号"></el-table-column>
    <el-table-column property="orderNo" label="单号"></el-table-column>
    <el-table-column property="name" label="菜品名称"></el-table-column>
    <el-table-column property="atchType" label="菜品类型"  width="80"></el-table-column>
    <el-table-column label="下单时间">
      <template slot-scope="scope">
          {{ parseTime(scope.row.callUpTime) }}
        </template>
    </el-table-column>
    <el-table-column label="配菜时间">
      <template slot-scope="scope">
          {{ parseTime(scope.row.cookFinishTime) }}
        </template>
    </el-table-column>
    <el-table-column label="传菜时间">
      <template slot-scope="scope">
          {{ parseTime(scope.row.serveFinishTime) }}
        </template>
    </el-table-column>
    <el-table-column property="totalTime" label="出菜耗时" width="80"></el-table-column>
    <el-table-column property="cookTime" label="配菜耗时"  width="80"></el-table-column>
    <el-table-column property="serveTime" label="传菜耗时"  width="80"></el-table-column>
    <el-table-column property="serveTime" label="是否被迫异常"  width="80">
      <template slot-scope="scope">
        <span v-if="scope.row.timeOutType=='1'">否</span>
        <span v-if="scope.row.timeOutType=='2'">是</span>
      </template>
    </el-table-column>
  </el-table>
  <pagination
      :total="kscTotal"
      :show="kscTotal > 0"
      :page.sync="kscQueryParams.pageNum"
      :limit.sync="kscQueryParams.pageSize"
      @pagination="getKscList"
    />
</el-dialog>
  </div>
</template>
<script>
import lineChart from "@/views/dashboard/LineChart.vue";
import {
  getBsKscHourList,
  buildHourDataChart,
  buildHourAvgTimeDataChart,
} from "@/api/liangshi/bsKscHour";
import {getKscAndArchItemInfoByDate} from '@/api/liangshi/kcKcs'
import { treeselect as listDept } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { lineChart,Treeselect },
  data() {
    return {
      dialogTableVisible:false,
      gridData: [],
       // 遮罩层
      loading: false,
      showDishesCount: false,
      showDishesAvgTime: false,
      dishesCountChartData: [],
      dishesAvgTimeChartData: [],
      deptList:[],
      dishesCountChartConfig: {
        xAxisData: [],
        legendData: [
          "每小时出餐数量",
          "每小时被迫出餐数量",
          "A类菜品出餐数量",
          "B类菜品出餐数量",
          "C类菜品出餐数量",
        ],
      },
      dishesAvgTimeChartConfig: {
        xAxisData: [],
        legendData: [
          "每小时平均出餐时间",
          "A类菜品平均出餐时间",
          "B类菜品平均出餐时间",
          "C类菜品平均出餐时间",
        ],
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,

      },
      kscQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      kscTotal:0,
      tableData: [],
    };
  },
  created() {
   this.initData();
   this.getDeptList();
  },
  methods: {
    getKscList(){
      getKscAndArchItemInfoByDate(this.kscQueryParams).then(res=>{
        this.gridData=res.rows;
        this.kscTotal=res.total;
        this.dialogTableVisible=true
      })
    },
    queryDishesInfo(itemType,outType,item){
      console.log(itemType,outType,item)
      this.kscQueryParams.date=item.date;
      this.kscQueryParams.time=item.hour;
      this.kscQueryParams.shopId=this.queryParams.shopId;
      this.getKscList();
    },
    initData(){
      this.getList();
    this.getDishesCountChartData();
    this.getDishesAvgTimeChartData();
    },
    getDeptList(){
      listDept().then(res=>{
        this.deptList=res.data;
      })
    },
    getList() {
      this.loading=true;
      getBsKscHourList(this.queryParams).then((res) => {
        this.loading=false;
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    getDishesCountChartData() {
      this.showDishesCount = false;
      this.dishesCountChartData=[]
      buildHourDataChart(this.queryParams).then((res) => {
        this.dishesCountChartConfig.xAxisData = res.hoursData;
        this.dishesCountChartData.push({
          name: "每小时出餐数量",
          data: res.dishesTotalCount,
        });
        this.dishesCountChartData.push({
          name: "每小时被迫出餐数量",
          data: res.timeOutDishesCount,
        });
        this.dishesCountChartData.push({
          name: "A类菜品出餐数量",
          data: res.aDishesCount,
        });
        this.dishesCountChartData.push({
          name: "B类菜品出餐数量",
          data: res.bDishesCount,
        });
        this.dishesCountChartData.push({
          name: "C类菜品出餐数量",
          data: res.cDishesCount,
        });
        this.showDishesCount = true;
      });
    },
    getDishesAvgTimeChartData() {
      this.showDishesAvgTime = false;
      this.dishesAvgTimeChartData=[]
      buildHourAvgTimeDataChart(this.queryParams).then((res) => {
        console.log(res);
        this.dishesAvgTimeChartConfig.xAxisData = res.hoursData;
        this.dishesAvgTimeChartData.push({
          name: "每小时平均出餐时间",
          data: res.avgTotalTime,
        });
        this.dishesAvgTimeChartData.push({
          name: "A类菜品平均出餐时间",
          data: res.avgATotalTime,
        });
        this.dishesAvgTimeChartData.push({
          name: "B类菜品平均出餐时间",
          data: res.avgBTotalTime,
        });
        this.dishesAvgTimeChartData.push({
          name: "C类菜品平均出餐时间",
          data: res.avgCTotalTime,
        });
        this.showDishesAvgTime = true;
        console.log("dishesAvgTimeChartData",this.dishesAvgTimeChartData)
      });
    },
  },
};
</script>

<style>
</style>